<?php
declare (strict_types = 1);

namespace app\common\model;

use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;
class SchoolPettyCash extends Model
{
    use SoftDelete;
     protected $deleteTime = false;
    // 获取列表
    public static function getList()
    {
        $where = [];
        $limit = input('get.limit');
        $admin = session('admin');
        if ($admin['school_id'] > 0) {
            $where[] = ['school_id', '=', $admin['school_id']];
        }
        $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc')?:['role_id'=>1];
        // $users = AdminRole::where('admin_id', $admin['id'])->find();
        if ($users['role_id'] > 2) {
            //招生主管||教学主管
            if ($users['role_id']==5||$users['role_id']==6){
                $admin_id = self::getSubordinateId($admin['id']);
                $where[] = ['admin_id','in',$admin_id];
            }

            //财务
            if ($users['role_id']==4){
                //主管审核通过
                $where[] = ['director_audit','=',2];
            }

            //投资人
            if ($users['role_id']==8){
                //主管审核通过
                $where[] = ['director_audit','=',2];
                //财务审核通过
                $where[] = ['finance_audit','=',2];
            }

//            $where[] = ['admin_id', '=', $admin['id']];
        }
        $list = self::with([
            'get_admin' => function($a) {
                $a->field('id,username');
            },'get_school' => function($a) {
                $a->field('id,school_name');
            }
        ])->order('id','desc')->where($where)->paginate($limit);
        $items = $list->items();
        foreach ($items as $k => $v) {
            $users = dataFind('bl_admin_admin_role', ['admin_id'=> $admin['id']], '*', 'id desc');
            if ($users) {
                $items[$k]['group'] = AdminRole::where('id', $users['role_id'])->value('name');
            } else {
                $items[$k]['group'] = '未分组';
            }

        }


        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
    //获取当前角色下的用户id
    public static function getSubordinateId($admin_id){
        return  Db::table('bl_admin_admin')
            ->where(['top_id'=>$admin_id])
            ->column('id');
    }

    public function getAdmin()
    {
        return $this->hasOne(AdminAdmin::class, 'id', 'admin_id');
    }

    public function getSchool()
    {
        return $this->hasOne(SchoolIndex::class, 'id', 'school_id');
    }

}
